package com.stormragetech.sylwanas.persistence;

import lombok.ToString;
import lombok.extern.log4j.Log4j2;

import java.io.*;

/**
 * description: //TODO
 *
 * @author: Xue Bin
 * @version: 1.0.0-SNAPSHOT
 */
@Log4j2
public class FilePersistExecuter extends FilePersistable implements Persistable {

    private String path;
    private String name;

    public FilePersistExecuter(String path, String name) {
        this.path = path;
        this.name = name;
    }

    @Override
    public void save(String source) {
        File f = new File(path, name);
        if (!f.exists() || !f.isFile()) {
            f = makeFile(path, name);
        }

        PrintStream ps = null;
        try {
            OutputStream os = new FileOutputStream(f, true);
            ps = new PrintStream(os, true);
            ps.println(source);
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ps.close();
        }
    }
}
